<?php
/**
 * 计算某个月最后一天的方法：
 * 计算后一个月年和月，让后用后一个月的日期-1天
 * 例如要计算2009年2月的最后一天，
 * 先计算2009年3月1日的时间戳，减去24小时得到2月的最后一天日期。
 * */

class mydate{

	//通过从0到6的数组，返回中文的星期字符串
	public  static function getWeekInfo($week=[]){
		$myWeek = [];//转换星期天为7 周一到周六对应1-6
		for($i = count($week)-1; $i >=0 ; $i-- ){
			if($week[$i]==0) $week[$i] = 7;//转换星期天为7
			if($week[$i]>0 && $week[$i]<=7){
				$myWeek[] = $week[$i];
			}
		}

		$myWeek = array_unique($myWeek);
		sort($myWeek);
		if(count($myWeek)==7) {
			return "每天";
		}else if(count($myWeek)==6) {
			if(!in_array(7,$myWeek)){
				return "除周日外";
			}else if(!in_array(1,$myWeek)){
				return "除周一外";
			}else if(!in_array(2,$myWeek)){
				return "除周二外";
			}else if(!in_array(3,$myWeek)){
				return "除周三外";
			}else if(!in_array(4,$myWeek)){
				return "除周四外";
			}else if(!in_array(5,$myWeek)){
				return "除周五外";
			}else if(!in_array(6,$myWeek)){
				return "除周六外";
			}
		}else if(count($myWeek)==5) {
			if(!in_array(6,$myWeek) && !in_array(7,$myWeek)){
				return "周一到周五";
			}else{
				$except = [];
				for($i = 1; $i <=7; $i++){
					if(!in_array($i,$myWeek)){
						$except[] = $i;
					}
				}
				return "除周".self::getCnWeekDay($except[0])."和周".self::getCnWeekDay($except[1])."";
			}
		}

		$cnWeek = [];
		for($i = 0; $i < count($myWeek); $i++){
			$cnWeek[] = self::getCnWeekDay($myWeek[$i]);
		}
		if(count($myWeek)==2) {
			return "周".$cnWeek[0]."和周".$cnWeek[1];
		}


		return "周".join("、",$cnWeek);
	}

	public static function getCnWeekDay($num){
		switch($num){
			case 1:
				return "一";
			break;
			case 2:
				return "二";
			break;
			case 3:
				return "三";
			break;
			case 4:
				return "四";
			break;
			case 5:
				return "五";
			break;
			case 6:
				return "六";
			break;
			case 0:
			case 7:
				return "日";
			break;

		}
	}

	//获取中文的时间
	public static function getCnTime($dateAndTime){
		$time = strtotime($dateAndTime);
		$today = date("Y-m-d");
		$yesterday = date("Y-m-d",time()-24*3600);
		if($dateAndTime>= $today." 00:00:00" &&  $dateAndTime<= $today." 05:00:00" ){
			return "凌晨".date("H:i",$time);
		}else if($dateAndTime>= $today." 05:00:00" &&  $dateAndTime<= $today." 08:00:00" ){
			return "早上".date("H:i",$time);
		}else if($dateAndTime>= $today." 08:00:00" &&  $dateAndTime<= $today." 12:00:00" ){
			return "上午".date("H:i",$time);
		}else if($dateAndTime>= $today." 12:00:00" &&  $dateAndTime<= $today." 14:00:00" ){
			return "中午".date("H:i",$time);
		}else if($dateAndTime>= $today." 14:00:00" &&  $dateAndTime<= $today." 18:00:00" ){
			return "下午".date("H:i",$time);
		}else if($dateAndTime>= $today." 18:00:00" &&  $dateAndTime<= $today." 23:00:00" ){
			return "晚上".date("H:i",$time);
		}else if($dateAndTime>= $today." 23:00:00" &&  $dateAndTime<= $today." 23:59:59" ){
			return "深夜".date("H:i",$time);
		}else if($dateAndTime>= $yesterday." 00:00:00" && $dateAndTime<= $yesterday." 23:59:59"){
			return "昨天";
		}else{
			return date("m-d",$time);
		}
	}
	
	/*
	 * @param $date 形如20120818转换为2012-08-18
	 * */
	public static function  sortToLong($date){
		return substr($date,0,4).'-'.substr($date,4,2).'-'.substr($date,6,2);
	}
	
	/*
	 * 根据传入的日期，获取短格式的日期
	 * @param $date 日期（形如2009/8/1不用带时间）
	 * */
	public static function  getShortDate($date){
		$time = $date.' 00:00:00';
		return date('Ymd',strtotime($time));
	}
	
	/*
	 * @param $str 月份的英文
	 * @return $month 月份的数字
	 * */
	public static function getMonthNum($str){
		$istr = trim(strtolower($str));
		switch ($istr){
			case 'january':
				return '01';
				break;
			case 'february':
				return '02';
				break;
			case 'march':
				return '03';
				break;
			case 'april':
				return '04';
				break;
			case 'may':
				return '05';
				break;
			case 'june':
				return '06';
				break;
			case 'july':
				return '07';
				break;
			case 'august':
				return '08';
				break;
			case 'september':
				return '09';
				break;
			case 'october':
				return '10';
				break;
			case 'november':
				return '11';
				break;
			case 'december':
				return '12';
				break;
			default:
				return 0; //有错误
		}
	}
	
	/*
	 * 获取某月最后一天
	 * @param $year 年，例如 2009
	 * @param $month 月，例如 12
	 * @return 返回形如20110818，当月最后一天
	 * */
	public  static function getLastDateOfMonth($year,$month){
		$time = strtotime($year.'-'.$month.'-01'.' 00:00:00')	;
		return date('Ymt',$time);
	}
	
	/**
	 * 将8位日期转换为带-号的10为日期，20080808=>2008-08-08
	 * */
	
	
}